package com.lpz.controller;

import com.lpz.damian.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/anno")
@SessionAttributes("msg")//msg中存在了session中
public class AnnoController {
    @RequestMapping("/testRequestParam")
    private String testRequestParam(@RequestParam("name") String username){
        System.out.println(">>>>>>>>>"+username);
        return "success";
    }
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println(">>>>>>>"+body);
        return "success";
    }
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(name = "id") String id){
        System.out.println(">>>>>>>>>>>>>>"+id);
        return "success";
    }
    //获取请求头信息,用的比较少
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "accept") String id){
        System.out.println(">>>>>>>>>>>>>>"+id);
        return "success";
    }
    //获取cookie的值
    @RequestMapping("/testCookievalue")
    public String testCookievalue(@CookieValue("JSESSIONID") String cookie){
        System.out.println(">>>>>>>>>"+cookie);
        return "success";
    }
    //获取cookie的值
    @RequestMapping("/testMoodelAttribute")
    public String testMoodelAttribute(@ModelAttribute("id") User user){//取到id给user对象
        System.out.println(">>>>>>>>>"+user);
        return "success";
    }
    //@ModelAttribute
    public void showuser(String username, Map<String,User> maps){
        User user=new User();
        user.setTest("10");
        user.setName(username);
        user.setBirthday(new Date());
        maps.put("id",user);
        System.out.println(">>>>>>>ModelAttribute注解应用在方法上"+user);
    }
    /*public User showuser(String username,String age){
        User user=new User();
        user.setTest(age);
        user.setName(username);
        user.setBirthday(new Date());
        System.out.println(">>>>>>>ModelAttribute注解应用在方法上"+user);
        return user;
    }*/
    //Model接口
    @RequestMapping("/testSessionattributes")
    public String testSessionattributes(Model model){//存值
        //存储到request域对象是一个map。然后在跳转成功的页面进行取值：${requestScope}
        model.addAttribute("msg","妹妹");
        System.out.println(">>>>>>>>>>开始存值"+model);
        return "success";
    }
    @RequestMapping("/getSessionattribute")
    public String getSessionattribute(ModelMap modelMap){//得到值
        String modelMaps=(String)modelMap.get("msg");
        System.out.println(">>>>>>>>>>开始取值"+modelMaps);
        return "success";
    }
    @RequestMapping("/delSessionattribute")
    public String delSessionattribute(SessionStatus status){//删除，也就是改变状态
        status.setComplete();
        System.out.println(">>>>>>>>>>删除完成"+status);
        return "success";
    }


}
